package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.PriceItem;
import com.chuangyang.fixboxmaster.bean.SkillItem;
import com.chuangyang.fixboxmaster.ui.widgets.PinnedSectionListView;

import java.util.ArrayList;

public class SkillSectionedAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private ArrayList<SkillItem> mMasterSkill = new ArrayList<>();


    public SkillSectionedAdapter(ArrayList<SkillItem> skills){
        mMasterSkill = skills;
    }
    @Override
    public int getCount() {
        return mMasterSkill.size();
    }

    @Override
    public SkillItem getItem(int position) {
        return mMasterSkill != null ? mMasterSkill.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.master_skill_list_item, parent, false);
            holder.mSection = (TextView)convertView.findViewById(R.id.section);
            holder.mItem = (LinearLayout)convertView.findViewById(R.id.item);
            holder.mItemTitle = (TextView)convertView.findViewById(R.id.item_title);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        SkillItem item = getItem(position);

        if (item != null)
        {

            if (item.type == PriceItem.SECTION)
            {
                holder.mSection.setVisibility(View.VISIBLE);
                holder.mItem.setVisibility(View.GONE);
                holder.mSection.setText(item.skillTitle);
            }
            else if (item.type == PriceItem.ITEM)
            {
                holder.mItem.setVisibility(View.VISIBLE);
                holder.mSection.setVisibility(View.GONE);
                holder.mItemTitle.setText(item.skillTitle);
            }
        }
        return convertView;
    }

    public static class ViewHolder{
        public TextView mSection;

        public LinearLayout mItem;
        public TextView mItemTitle;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position) != null ? mMasterSkill.get(position).type : 0;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == PriceItem.SECTION;
    }
}
